package com.lagou.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NioClient {

    public static void main(String[] args) throws IOException {
        SocketChannel channel = SocketChannel.open();
        boolean connect = channel.connect(new InetSocketAddress("127.0.0.1", 9000));

        if (!connect) {
            while (!channel.finishConnect()) {
                System.out.println("由于连接需要事件， 客户端不会阻塞， 可以做其他工作！");
            }
        }

        // 如果连接成功就发送数据

        String msg = "hello world !";
        // Wraps a byte array into a buffer.
        ByteBuffer wrap = ByteBuffer.wrap(msg.getBytes());

        channel.write(wrap);
        // 阻塞
        System.in.read();



    }
}
